//
//  countTime.swift
//  Motion
//
//  Created by 悦月越悦 on 2021/3/19.
//

import SwiftUI
import Combine

struct CountTime: View {
    @EnvironmentObject var videoPlayModel: VideoPlayModel
    
    @State private var isActive = true
    @State var timePassing = 0
    
    @State var timer: Timer? = nil
    
                    
    var body: some View {
        VStack{
            Text("累计运动")
                .customFont(20, true)
                .padding(.bottom,4)
            Text(getTimeString())
                .customFont(20, true)
                .foregroundColor(Color("topicPurple"))
        }
        .onAppear(){
            timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true){ tempTimer in
                if (videoPlayModel.play){
                    timePassing += 1
                }
            }
        }
        .onDisappear(){
            timer?.invalidate()
        }
    }
    
    func getTimeString() -> String {
        let m = Int(timePassing / 60)
        let s = timePassing - m * 60
        return String(format: "%02d:%02d", arguments: [m, s])
    }
}

struct countTime_Previews: PreviewProvider {
    static var previews: some View {
        CountTime()
    }
}
